/**
 * @file CRhoAlgCommon.cpp Implementácia triedy CRhoAlgCommon.
 *
 * @author Martin Kosorinský.
 * @date 1.11.2009.
 */

#include "CRhoAlgCommon.hpp"

bool CRhoAlgCommon::computeIndex(const big aj, const big bj, const big ak, const big bk, big vysledok) {
    tracer << "Riesim koliziu: (aj,bj,ak,bk,n) = (" << aj << "," << bj << "," << ak << "," << bk << "," << ord << ")" << endl;
    bool retVal = false;
    big da = mirvar(0);
    big db = mirvar(0);
    subtract(aj, ak, da);
    subtract(bk, bj, db);

    if (xgcd(db, ord, db, db, db) == 1) {
        mad(da, db, da, ord, ord, vysledok);
        add(vysledok, ord, vysledok);
        divide(vysledok, ord, ord);
        retVal = true;
    }
    else {
        tracer << "Zlyhala podmienka s gcd v 'computeIndex'!" << endl;
        retVal = false;
    }
    mirkill(da);
    mirkill(db);
    return retVal;
}

void CRhoAlgCommon::makeStep(epoint * X, big a, big b, big tmp) {

    epoint_get(X, tmp, tmp);
    long rnd = remain(tmp, 3);

    if (rnd == 0) {
        incr(a, 1, a);
        divide(a, ord, ord);
        ecurve_add(G, X);
    }
    else if (rnd == 1) {
        add(a, a, a);
        divide(a, ord, ord);
        add(b, b, b);
        divide(b, ord, ord);
        ecurve_double(X);
    }
    else {
        incr(b, 1, b);
        divide(b, ord, ord);
        ecurve_add(H, X);
    }
}

bool CRhoAlgCommon::checkIndex(big dl) {
    bool retVal = false;

    if (!isInitialized()) {
        cerr << "Error: '" << __FUNCTION__ << "': Nie su inicializovane parametre krivky (kontext)!" << endl;
        return false;
    }
    epoint * F = epoint_init();
    ecurve_mult(dl, G, F);
    retVal = epoint_comp(H, F);

    epoint_free(F);
    return retVal;
}
